<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/9/5
 * Time: 14:50
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Match extends Base {

    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $where = [];
            if(isset($user_name) && $user_name != ''){
                $where['match_title'] = ['like', '%' . $user_name . '%'];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('match')->where($where)->count();// 获取总条数
            $lists = Db::name('match')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('creat_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /**
     * 删除
     * @return \think\response\Json
     */
    public function del(){
        $id = input('id');
        $res = Db::table('up_match')->where('match_id', $id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }

    /**
     * 上架
     * @return \think\response\Json
     */
    public function bannerOn(){
        $banner_id = input('id');
        $res = Db::table('up_match')->where('match_id', $banner_id)->setField('match_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '上架成功']);
        }else{
            return json(['code' => 200, 'msg' => '上架失败']);
        }
    }

    /**
     * 下架
     * @return \think\response\Json
     */
    public function bannerDown(){
        $banner_id = input('id');
        $res = Db::table('up_match')->where('match_id', $banner_id)->setField('match_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '下架成功']);
        }else{
            return json(['code' => 200, 'msg' => '下架失败']);
        }
    }

    /**
     * 添加banner
     * @return mixed|\think\response\Json
     */
    public function add(){
        if(Request::instance()->isPost()){
            $banner_title = trim(input('banner_title'), ' ');
            $banner_status = input('banner_status');
            $content= input('assn_join');
            $data = [
                'match_title' => $banner_title,
                'match_status' => $banner_status,
                'creat_time' => date('Y-m-d H:i:s'),
                'match_content' => $content,
            ];
            $res = Db::table('up_match')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }

    public function edit(){
        if(Request::instance()->isPost()){
            $id = input('id');
            $banner_title = trim(input('banner_title'), ' ');
            $content= input('assn_join');
            $data = [
                'match_title' => $banner_title,
                'creat_time' => date('Y-m-d H:i:s'),
                'match_content' => $content,
            ];
            $res = Db::table('up_match')->where('match_id', $id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $banner_id = input('id');
        // banner
        $banner = Db::table('up_match')
            ->where('match_id', $banner_id)
            ->find();

        $this->assign([
            'match_id' => $banner_id,
            'banner' => $banner
        ]);
        return $this->fetch();
    }
}